﻿
using FlowSequencer.Core.FlowElements;
using FlowSequencer.Model;
using FlowSequencer.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace FlowSequencer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MachineLineViewModel context;
     
        public MainWindow()
        {
            InitializeComponent();
            context = new MachineLineViewModel(6, 3);
            this.DataContext = context;
        }

        private void startProcessingButton_Click_1(object sender, RoutedEventArgs e)
        {
            
            var flow = context.GetOptimalSequence();
            var maxCycles =  context.Lines[0].ColumnsData.Max(x => int.Parse(x.Data)) + 1;

            var numberOfMachines = 3;// Machine2xRadioButton.IsChecked == true ? 2 : 3;
            int cycles;
            for (int i = 0; i < flow.Lines.Count; i++)
			{
                if (i == 0)
                    cycles = DisplayTasks(flow.Lines[i].Tasks, new List<Task>(), i);
                else
                {
                    cycles = DisplayTasks(flow.Lines[i].Tasks, flow.Lines[i - 1].Tasks, i);
                }
                maxCycles = maxCycles > cycles ? maxCycles : cycles;
			}
            FormatGanttView(numberOfMachines, maxCycles);
            for (int i = 0; i < flow.Lines.Count; i++)
            {
                if (i == 0)
                    cycles = DisplayTasks(flow.Lines[i].Tasks, new List<Task>(), i);
                else
                {
                    cycles = DisplayTasks(flow.Lines[i].Tasks, flow.Lines[i - 1].Tasks, i);
                }
            }
            CreateMesureRumble(maxCycles, 4);        
        }

        private Brush PickBrush(int i)
        {
            Type brushesType = typeof(System.Windows.Media.Brushes);
            var properties = brushesType.GetProperties(BindingFlags.Static | BindingFlags.Public);
            string name = properties[i].Name;
            SolidColorBrush brush = (SolidColorBrush)properties[i*3].GetValue(null, null);
            return brush;
        }

        private void CreateMesureRumble(int maxCycles, int rowNumber)
        {
            for (int i = 0; i < maxCycles; i++)
            {
                TextBlock tblock = new TextBlock();
                tblock.FontSize = 14;
             
                if(i % 5 == 0)
                    tblock.Text = i.ToString();
                tblock.TextAlignment = TextAlignment.Left;
                Grid.SetRow(tblock, rowNumber);
                Grid.SetColumn(tblock, i);
                ganttView.Children.Add(tblock);
            }
        }
        
        private int DisplayTasks(List<Task> tasks, List<Task> previousTasks, int rowNumber)
        {
            int currentColumn = 0;
            for (int i = 0; i < tasks.Count; i++)
            {
                Button tblock = new Button();
                if(tasks[i].Index >= 0)
                    tblock.Background = PickBrush(tasks[i].Index);
                tblock.FontSize = 20;
                tblock.Content = tasks[i].Name;
         
                Grid.SetRow(tblock, rowNumber);
                Grid.SetColumn(tblock, currentColumn);
                if (tasks[i].Cycles > 1)
                    Grid.SetColumnSpan(tblock, tasks[i].Cycles);
                if(tasks[i].Index != -1)
                    ganttView.Children.Add(tblock);
                currentColumn = tasks[i].Cycles + currentColumn;
            }
            return currentColumn;
        }

        private void FormatGanttView(int rows, int columns)
        {
            ganttView.ColumnDefinitions.Clear();
            ganttView.RowDefinitions.Clear();
            ganttView.Children.Clear();
            for (int i = 0; i < columns; i++)
            {
                ColumnDefinition c = new ColumnDefinition();
                c.Width = new GridLength(15, GridUnitType.Pixel);
                ganttView.ColumnDefinitions.Add(c);
            }
            for (int i = 0; i < rows + 2; i++)
            {
                RowDefinition r = new RowDefinition();
                ganttView.RowDefinitions.Add(r);
            }
        }

        private void RefreshTable()
        {
            var numberOfMachines = context.IsTwoMachineMode == true ? 2 : 3;
            context.UpdateModel(context.NumberOfTasks, numberOfMachines);
        }
        
        private void numberTasksTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            RefreshTable();
        }

        private void Machine3xRadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            RefreshTable();
        }

        private void Machine2xRadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            RefreshTable();
        }

  
    
    }
}
